import 'package:base_flutter/common/api/api_exception.dart';
import 'package:base_flutter/common/api/dio_util.dart';
import 'package:base_flutter/common/global.dart';
import 'package:base_flutter/common/models/home.dart';
import 'package:base_flutter/models/index.dart';
import 'package:dio/dio.dart';

import 'api_response.dart';

class Api {
  /// 登录
  static Future<ApiResponse<User>?> login(String phone, String code) async {
    /// json => model
    try {
      var ret = await dioUtil.request("api/v1/phoneLogin",
          method: DioMethod.post, data: {'phone': phone, 'code': code});
      if (ret['code'] == 200) {
        var user = User.fromJson(ret['data']);
        return ApiResponse(code: 200, data: user);
      } else {
        return ApiResponse(code: 400, msg: ret['msg']);
      }
    } catch (e) {
      debugInfo('error: $e');
    }
    return null;
  }

  /// 发送验证码
  ///
  static Future<bool> sendCode(String phone) async {
    var ret = await dioUtil.request('api/v1/sendCode',
        method: DioMethod.post, data: {'phone': phone});
    return true;
  }

  /// 获取用户信息
  ///
  static Future<User?> getUserInfo() async {
    return null;
  }

  /// 首页数据
  ///
  static Future<ApiResponse<HomeModel>?> getHomeData(String cityCode) async {
    try {
      var ret = await dioUtil.request("api/v1/getHomeInfo",
          method: DioMethod.post, data: {'city_code': cityCode});

      if (ret['code'] == 200) {
        List<Banner> banners = (ret['data']['slide'] as List)
            .map((e) => Banner.fromJson(e))
            .toList();
        List<Jishi> jishi = (ret['data']['technician_pecommend'] as List)
            .map((e) => Jishi.fromJson(e))
            .toList();
        List<Fuwu> fuwu = (ret['data']['service_pecommend'] as List)
            .map((e) => Fuwu.fromJson(e))
            .toList();

        return ApiResponse(
            code: 200,
            data: HomeModel(
                banners: banners, recomentJishi: jishi, recomentFuwu: fuwu));
      } else {
        return ApiResponse(code: 400, msg: ret['msg']);
      }
    } catch (e) {
      debugInfo('error: $e');
    }
    return null;
  }

  /// 技师列表
  /// order 排序：0综合排序 1.距离近 2距离远 3.销量高 4销量低
  /// workStatus 上班状态：1.休息 2.上班 3.服务中
  /// sex 性别：1.男 2 女
  static Future<List<Jishi>?> getNurseData(
      {int page = 1,
      int pageSize = 20,
      String cityCode = '500100',
      String lat = '30.829606',
      String lon = '104.168738',
      String order = '0',
      String? workStatus,
      String? sex}) async {
    try {
      Map<String, String> parmas = {
        'city_code': cityCode,
        'lng': lon,
        'lat': lat,
        'order': order
      };
      if (workStatus != null) {
        parmas['work_status'] = workStatus;
      }
      if (sex != null) {
        parmas['sex'] = sex;
      }
      var ret = await dioUtil.request('api/v1/getTechnicianList', data: parmas, method: DioMethod.post);

      if (ret['code'] == 200 && (ret['data']['data'] as List).isNotEmpty) {

        return (ret['data']['data'] as List).map((e) => Jishi.fromJson(e)).toList();
      }
      return null;
    } catch (e) {
      return null;
    }

  }

  /// 技师详情
  ///

  static Future<Jishi?> getJishiInfo(String id) async {
    try {
      var ret = await dioUtil.request("api/v1/getTechnicianInfo",
          method: DioMethod.post, data: {'id': id});
      if (ret['code'] == 200) {

        return Jishi.fromJson(ret['data']);
      } else {
        return null;
      }
    }catch (e) {
      debugInfo('技师详情 $e');
      return null;
    }
    return null;
  }

}
